document.addEventListener("DOMContentLoaded", function () {
    // 显示/隐藏密码
    const togglePassword = document.getElementById("togglePassword");
    const password = document.getElementById("password");

    togglePassword.addEventListener("click", function () {
        const type =
            password.getAttribute("type") === "password" ? "text" : "password";
        password.setAttribute("type", type);
        this.innerHTML =
            type === "password"
                ? '<i class="fas fa-eye"></i>'
                : '<i class="fas fa-eye-slash"></i>';
    });

    // 表单验证
    const loginForm = document.getElementById("loginForm");
    const usernameError = document.getElementById("usernameError");
    const passwordError = document.getElementById("passwordError");

    loginForm.addEventListener("submit", function (e) {
        e.preventDefault();

        // 重置错误信息
        usernameError.textContent = "";
        passwordError.textContent = "";

        const username = document.getElementById("username").value.trim();
        const password = document.getElementById("password").value;

        let isValid = true;

        // 用户名验证
        if (username === "") {
            usernameError.textContent = "请输入用户名或邮箱";
            isValid = false;
        }

        // 密码验证
        if (password === "") {
            passwordError.textContent = "请输入密码";
            isValid = false;
        } else if (password.length < 6) {
            passwordError.textContent = "密码长度不能少于6个字符";
            isValid = false;
        }

        if (isValid) {
            // 模拟登录请求
            simulateLogin(username, password);
        }
    });

    function simulateLogin(username, password) {
        // 显示加载状态
        const loginBtn = document.querySelector(".btn-login");
        const originalText = loginBtn.innerHTML;
        loginBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 登录中...';
        loginBtn.disabled = true;

        // 从localStorage获取用户数据
        const users = JSON.parse(localStorage.getItem("users")) || [];

        // 查找匹配的用户
        const user = users.find(
            (u) =>
                (u.username === username || u.email === username) &&
                u.password === password
        );

        // 模拟API请求延迟
        setTimeout(() => {
            if (user) {
                // 登录成功
                alert("登录成功！");

                // 保存登录状态和用户信息
                localStorage.setItem("isLoggedIn", "true");
                localStorage.setItem(
                    "currentUser",
                    JSON.stringify({
                        username: user.username,
                        email: user.email,
                    })
                );

                // 重定向到首页
                window.location.href = "index.html";
            } else {
                // 登录失败
                alert("用户名或密码错误！");

                // 恢复按钮状态
                loginBtn.innerHTML = originalText;
                loginBtn.disabled = false;
            }
        }, 1000);
    }

    // 检查是否已登录
    checkLoginStatus();
});

// 检查登录状态的函数，可以在其他页面使用
function checkLoginStatus() {
    const isLoggedIn = localStorage.getItem("isLoggedIn");
    if (isLoggedIn === "true") {
        // 用户已登录，可以更新UI显示登录状态
        console.log("用户已登录");
    }
}

// 注销函数
function logout() {
    localStorage.removeItem("isLoggedIn");
    window.location.href = "login.html";
}
